﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class LiveTop : UserControl
    {
        internal WidgetServiceProxy ServiceProxy;
        internal WidgetSpaceProxy SpaceProxy;
        private LiveTopBG m_bg = new LiveTopBG();
        private LiveTopDesc m_desc = new LiveTopDesc();

        public LiveTop()
        {
            InitializeComponent();
            if (WidgetHelper.IsInMySpace == false)
                this.Visibility = Visibility.Collapsed;
        }

        public bool Active
        {
            get
            {
                return m_desc.Active;
            }
            set
            {
                string ret;
                m_desc.Url = TxtUrl.Text;
                m_desc.Active = value;
                if (value)
                {
                    string url = m_desc.Url;
                    if (url.Contains(@"http://") == false)
                    {
                        url = WidgetHelper.MapFileUrl(
                            WidgetHelper.CurrentSpace.OwnerUserID, url);
                    }
                    if (url.Contains(@".xap"))//load silverlight
                        m_bg.SetXap(url);
                    else
                        m_bg.SetMedia(url);
                    SpaceProxy.SetSpaceBackground(m_bg);
                    BtnToggle.Content = "关闭LiveTop";
                }
                else
                {
                    SpaceProxy.SetSpaceBackground(null);
                    BtnToggle.Content = "开启LiveTop";
                }
                ret = WidgetHelper.ObjectToXml(m_desc, typeof(LiveTopDesc));
                ServiceProxy.SetSession("livetop", ret);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceProxy = OperationManager.Instance.CreateWidgetServiceProxy(this);
            SpaceProxy = OperationManager.Instance.CreateWidgetSpaceProxy(this);
            ServiceProxy.GetSessionCompleted += new EventHandler<DragDraw.UserService.GetSessionCompletedEventArgs>(OnGetSession);
            ServiceProxy.GetSession("livetop");
        }

        void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))
                return;
            try
            {
                m_desc = (LiveTopDesc)WidgetHelper.XmlToObject(e.Result, typeof(LiveTopDesc));
                TxtUrl.Text = m_desc.Url;
                if (m_desc.Active)
                {
                    Active = true;
                }
                else
                {
                    Active = false;
                }
            }
            catch { }
        }

        private void BtnToggle_Click(object sender, RoutedEventArgs e)
        {
            Active = !Active;
        }
    }

    public class LiveTopDesc
    {
        public bool Active { get; set; }
        public string Url { get; set; }
        public LiveTopDesc()
        {
            Active = false;
            Url = "";
        }
    }
}
